package com.lyy.weather.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.lyy.weather.activity.MainActivity;
import com.lyy.weather.network.HttpUtil;

public class DatabaHelper extends SQLiteOpenHelper {
private Context mContext;
private int version;
    private Handler handler;
//记录所有数据的上一次更新时间
private final static  String CREATE_TIMERECORD="create table TimeRecord("
        +"mid integer primary key autoincrement,"
        +"time text)";
//城市信息的建表语句
private final static String CREATE_CITY="create table City("
        +"mid integer primary key autoincrement,"
        +"name text,"
        +"id text,"
        +"adm2 text,"
        +"adm1 text,"
        +"country text)";
//实时天气信息的建表语句
private final static String CREATE_WEATHER_TIMELY="create table WeatherTimely("
        +"mid integer primary key autoincrement,"
        +"locationId text,"
        +"obsTime text,"
        +"temperature text,"
        +"feelsLike text,"
        +"icon integer,"
        +"text text,"
        +"wind360 text,"
        +"windDir text,"     +"windScale text,"      +"windSpeed text,"
        +"humidity text," +"precip real," +"pressure text,"+"vis text,"
        +"cloud text,"  +"dew text)";

//逐小时天气预报的建表语句
    private final static String CREATE_WEATHER_HOURLY="create table WeatherHourly("
            +"mid integer primary key autoincrement,"
            +"locationId text,"
            +"fxTime text,"
            +"temperature text,"
            +"icon integer,"
            +"text text,"
            +"wind360 text,"
            +"windDir text,"     +"windScale text,"      +"windSpeed text,"
            +"humidity text," +"precip text," +"pressure text,"+"pop text,"
            +"cloud text,"  +"dew text)";

    //逐天天气预报的建表语句
    private final static String CREATE_WEATHER_DAILY="create table WeatherDaily("
            +"mid integer primary key autoincrement,"
            +"locationId text,"
            +"fxDate text,"  +"sunrise text,"   +"sunset text," +"moonrise text,"   +"moonset text,"
            +"moonPhase text,"  +"moonPhaseIcon integer," +"tempMax text," +"tempMin text,"
            +"iconDay integer," +"textDay text," +"iconNight integer," +"textNight text,"
            +"wind360Day text," +"windDirDay text," +"windScaleDay text," +"windSpeedDay text,"
            +"humidity text," +"precip text," +"pressure text," +"vis text," +"cloud text," +"uvIndex text)";

    //天气生活指数的建表语句
    private final static String CREATE_LIFE_INDICES="create table LifeIndices("
            +"mid integer primary key autoincrement,"
            +"locationId text,"
            +"date text," +"type text," +"name text," +"level text," +"category text,"
            +"text text)";

    //空气质量指数的建表语句
    private final static String CREATE_AIR_INDICES="create table AirIndices("
            +"mid integer primary key autoincrement,"
            +"locationId text,"
            +"pubtime text," +"aqi text," +"level integer," +"category text,"
            +"mprimary text,"//对应的是primary
            +"pm10 text," +"pm2p5 text,"
            +"no2 text," +"so2 text,"
            +"co text," +"o3 text)";


    public DatabaHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, Handler handler) {
        super(context, name, factory, version);
        mContext=context;
        this.version=version;
        this.handler=handler;
    }

    public DatabaHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext=context;
        this.version=version;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_CITY);
        db.execSQL(CREATE_WEATHER_TIMELY);
        db.execSQL(CREATE_WEATHER_HOURLY);
        db.execSQL(CREATE_WEATHER_DAILY);
        db.execSQL(CREATE_LIFE_INDICES);
        db.execSQL(CREATE_AIR_INDICES);
        db.execSQL(CREATE_TIMERECORD);
        //第一次建表时，默认添加广州的城市信息

            /*    DatabaHelper dbHelper = new DatabaHelper(mContext, "WeatherInformation.db", null, version,handler);
                HttpUtil httpUtil=new HttpUtil("guangzhou",HttpUtil.MyRequestType.CITY_REQUEST,mContext,
                        dbHelper);
                httpUtil.getDataFromApi();*/


      //  Toast.makeText(mContext,"Create succeeded", Toast.LENGTH_SHORT)
           //     .show();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists City");
        db.execSQL("drop table if exists TimeRecord");
        db.execSQL("drop table if exists WeatherHourly");
        db.execSQL("drop table if exists WeatherTimely");
        db.execSQL("drop table if exists WeatherDaily");
        db.execSQL("drop table if exists LifeIndices");
        db.execSQL("drop table if exists AirIndices");
        onCreate(db);

    }
}
